const BehaviorAll = require("../../behaviors/behavior_all.js");
const ServiceProduct = require("../../service/service_product.js");
const ServicePosition = require("../../service/service_position.js");
const ServiceSpread = require("../../service/service_spread.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 级联相关
     */
    showedCascadePopup: false,
    cascadeInfo: '省份/地级市/县级市',
    /**
     * 联动相关
     */
    selectedTown: null,
    /**
     * 产品相关
     */
    products: [],
    types: ['红酒'],
    proxyImgUrl: null,
    /**
     * 开始页
     */
    startPage: 0,
    /**
     * fixAddress
     */
    fixAddress: null
  },
  /**
   * 
   */
  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      // 列出产品
      ServiceProduct.page(0, _this.data.types, [], {}, function (data) {
        _this.setData({ products: data.records });
      });
      ServiceSpread.getProxy(res => {
        _this.setData({ proxyImgUrl: res.imgUrl });
      })
    },
    /**
     * 打开弹出框
     */
    onPopupOpen: function () {
      const _this = this;
      _this.setData({ showedCascadePopup: true })
    },
    /**
     * 显示省份
     */
    onShowProvince: function(e){
      const _this = this;
      ServicePosition.provinceList(data => {
        e.detail.callback(data.records );
      })
    },
    /**
     * 显示地级市
     */
    onShowCity: function(e){
      const _this = this;
      ServicePosition.cityList(e.detail.level1.id, data => {
        e.detail.callback(data.records );
      })
    },
    /**
     * 显示县级市
     */
    onShowTown: function(e){
      const _this = this;
      ServicePosition.townList(e.detail.level2.id, null, data => {
        e.detail.callback(data.records );
      })
    },
    /**
     * 搜索
     */
    onSearch: function(e){
      const _this = this;
      ServicePosition.search(e.detail.search.split(" "), function (data) {
        // 转换为所需格式
        const arr = [];
        for(var item of data.records){
          arr.push({
            level1: { id: item.provinceId, name: item.provinceName },
            level2: { id: item.cityId, name: item.cityName },
            level3: { id: item.id, name: item.name }
          });
        }
        // 回调
        e.detail.callback(arr);
      });
    },
    /**
     * 显示产品
     */
    onListProduct: function(e){
      const _this = this;
      const province = e.detail.level1;
      const city = e.detail.level2;
      const town = e.detail.level3;
      ServiceProduct.pageByTown(0, _this.data.types, town.id, function (data) {
        e.detail.callback();
        _this.setData({
          products: data.records,
          selectedTown: town,
          cascadeInfo: province.name + '/' + city.name + '/' + town.name,
          fixAddress: province.name + '/' + city.name + '/' + town.name
        });
      });
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      const _this = this;
      let startPage = this.data.startPage + 1;
      const selectedTown = _this.data.selectedTown;
      ServiceProduct.pageByTown(startPage, _this.data.types, (selectedTown ? selectedTown.id: null), function (data) {
        _this.data.products.push(...data.records);
        _this.setData({
          products: _this.data.products,
          startPage: startPage
        });
      });
    }
  }

})